package eteam.aps.controller;

import eteam.aps.common.R;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.model.dto.WorkingProcedureQueryDto;
import eteam.aps.model.vo.working_procedure.WorkingProcedureShowVo;
import eteam.aps.model.vo.working_procedure.WorkingProcedureUpdateVo;
import eteam.aps.model.vo.working_procedure.WorkingProcedureVo;
import eteam.aps.service.WorkingProcedureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * 工序信息 控制层;
 *
 * @author LiuLei
 */
@Api(tags = "工序信息")
@RestController
@RequestMapping("/wpc")
public class WorkingProcedureController {

    @Autowired
    private WorkingProcedureService workingProcedureService;

    @PostMapping("/jsp")
    @ApiOperation("分页条件查询工序信息")
    @ApiImplicitParam(name = "JsonPage<WorkingProcedureShowVo>", value = "工序查询条件")
    public R<JsonPage<WorkingProcedureShowVo>> selWPJsp(@RequestBody WorkingProcedureQueryDto wpq) throws Exception {
        JsonPage<WorkingProcedureShowVo> jsonPage = workingProcedureService.selWPJsp(wpq);
        if (jsonPage.getTotalCount() == 0) throw new BusinessException(HttpStatus.BAD_REQUEST, "暂无数据！");
        return R.success(jsonPage);
    }

    @PostMapping("/simjsp")
    @ApiOperation("分页查询工序信息（工序）")
    @ApiImplicitParam(name = "jsonPage<WorkingProcedureVo>", value = "查询条件")
    public R<JsonPage<WorkingProcedureVo>> selWPSimJsp(@RequestBody WorkingProcedureQueryDto wpq) {
        JsonPage<WorkingProcedureVo> jsonPage = workingProcedureService.selWPSimJsp(wpq);
        if (jsonPage.getTotalCount() == 0) throw new BusinessException(HttpStatus.BAD_REQUEST, "暂无数据！");
        return R.success(jsonPage);
    }

    @PostMapping("/save")
    @ApiOperation("增加工序信息")
    @ApiImplicitParam(name = "WorkingProcedureUpdateVo", value = "新增条件")
    public R<Integer> saveWP(@RequestBody WorkingProcedureUpdateVo wpu) throws Exception {
        if (Helper.isEmpty(wpu.getfCode())) throw new BusinessException(HttpStatus.BAD_REQUEST, "工序编号不能为空!");
        if (Helper.isEmpty(wpu.getfName())) throw new BusinessException(HttpStatus.BAD_REQUEST, "工序名称不能为空!");
        if (Helper.isEmpty(wpu.getfDCode())) throw new BusinessException(HttpStatus.BAD_REQUEST, "工段编号不能为空!");
        if (Helper.isEmpty(wpu.getfDName())) throw new BusinessException(HttpStatus.BAD_REQUEST, "工段名称不能为空!");
        if (wpu.getfPreCheck() == null) throw new BusinessException(HttpStatus.BAD_REQUEST, "请输入是否检查前置工序!");
        if (wpu.getfPreCheck() == 1 && Helper.isEmpty(wpu.getfPreCheckCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "请选择前置工序的!");
        int count = workingProcedureService.selByProNameAndProScn(wpu.getfName(), wpu.getfCode());
        if (count != 0) throw new BusinessException(HttpStatus.BAD_REQUEST, "已存在工序信息");
        int save = workingProcedureService.saveWP(wpu);
        return R.success(save);
    }

    @PostMapping("/del/{fId}")
    @ApiOperation("删除工序信息")
    @ApiImplicitParam(name = "fId", value = "内码")
    public R<Integer> delWP(@PathVariable String fId) throws Exception {
        if (Helper.isEmpty(fId)) throw new BusinessException(HttpStatus.BAD_REQUEST, "fId 不能为空!");
        int del = workingProcedureService.delWP(fId);
        return R.success(del);
    }

    @PostMapping("/update")
    @ApiOperation("修改工序信息")
    @ApiImplicitParam(name = "WorkshopSectionUpdateVo", value = "修改条件")
    public R<Integer> updateWP(@RequestBody WorkingProcedureUpdateVo wpu) throws Exception {
        if (Helper.isEmpty(wpu.getfId())) throw new BusinessException(HttpStatus.BAD_REQUEST, "内码不能为空!");
        if (Helper.isEmpty(wpu.getfCode())) throw new BusinessException(HttpStatus.BAD_REQUEST, "工序编号不能为空!");
        if (Helper.isEmpty(wpu.getfName())) throw new BusinessException(HttpStatus.BAD_REQUEST, "工序名称不能为空!");
        if (Helper.isEmpty(wpu.getfDCode())) throw new BusinessException(HttpStatus.BAD_REQUEST, "工段编号不能为空!");
        if (Helper.isEmpty(wpu.getfDName())) throw new BusinessException(HttpStatus.BAD_REQUEST, "工段名称不能为空!");
        if (wpu.getfPreCheck() == null) throw new BusinessException(HttpStatus.BAD_REQUEST, "请输入是否检查前置工序!");
        if (wpu.getfPreCheck() == 1 && Helper.isEmpty(wpu.getfPreCheckCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "请选择前置工序的!");
        int update = workingProcedureService.updateWP(wpu);
        return R.success(update);
    }


}